package ar.edu.unlam.tpfinal.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.ForeignKey;

@Entity
@Table(name="TRANSACTIONS")
public class Transaction extends AbstractPersistentObject {
	
	public static String TRANSACTION_TYPE = "transactionType";
	
	private List<Client> clients;
	
	private TransactionType transactionType;
	
	private Date fromDate;
	
	private Date toDate;
	
	private Priority priority;
	
	private String comments;
	
	private TransactionStatus transactionStatus;

	@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@JoinTable(name = "TRANSACTION_CLIENT",
			   uniqueConstraints = { @UniqueConstraint(columnNames = {"TRANSACTION_OID", "CLIENT_OID"})},
			   joinColumns = { @JoinColumn(name = "TRANSACTION_OID", unique = false) },
			   inverseJoinColumns = { @JoinColumn(name = "CLIENT_OID", unique = false)})
	@ForeignKey(name="FK_TX_TO_CLIENT")
	public List<Client> getClients() {
		return clients;
	}

	public void setClients(List<Client> clients) {
		this.clients = clients;
	}
	
	public void setClient(Client client) {
		if(this.clients == null){
			this.clients = new ArrayList<Client>();
		}
		this.clients.add(client);
	}

	@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@JoinColumn(name = "TRANSACTION_TYPE_OID")
	@ForeignKey(name = "FK_TX_TO_TX_TYPE")
	public TransactionType getTransactionType() {
		return this.transactionType;
	}

	public void setTransactionType(TransactionType transactionType) {
		this.transactionType = transactionType;
	}

	@Column(name = "FROM_DATE", nullable = false, columnDefinition = "DATE")
	public Date getFromDate() {
		return this.fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	@Column(name = "TO_DATE", nullable = false, columnDefinition = "DATE")
	public Date getToDate() {
		return this.toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	@Column(name = "PRIORITY", nullable = false, columnDefinition = "INTEGER")
	public Priority getPriority() {
		return this.priority;
	}

	public void setPriority(Priority priority) {
		this.priority = priority;
	}

	@Column(name = "COMMENTS", columnDefinition="VARCHAR(254)", nullable = true)
	public String getComments() {
		return this.comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}
	
	@Column(name = "TRANSACTION_STATUS", nullable = false, columnDefinition = "INTEGER")
	public TransactionStatus getTransactionStatus() {
		return this.transactionStatus;
	}

	public void setTransactionStatus(TransactionStatus transactionStatus) {
		this.transactionStatus = transactionStatus;
	}
	
}
